import { Message } from 'iview';
import { getUser, findUserAllRole, findUserAllPermission, findModuleByUser, addUserModule, delUserModule } from '../../api/ServerApi';

const UserDetail = {
    namespaced: true,
    state() {
        return {
            pageLoading: false,
            userInfo: {
                username: null,
                nickname: null,
                userType: null,
                telephone: null,
                email: null,
                memberOf: null,
                description: null,
                ldapExist: null,
                enabled: null,
                locked: null,
                createAt: null,
                updateAt: null
            },
            userAllModule: [],
            rolesTableData: [],
            permissionTableData: [],
            addModal: {
                show: false,
                loading: false
            },
            addUserModule: { module: null }
        };
    },
    mutations: {
        setPageLoading(state, pageLoading) {
            state.pageLoading = pageLoading;
        },
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
        },
        setUserAllModule(state, userAllModule) {
            state.userAllModule = userAllModule;
        },
        setRolesTableData(state, rolesTableData) {
            state.rolesTableData = rolesTableData;
        },
        setPermissionTableData(state, permissionTableData) {
            state.permissionTableData = permissionTableData;
        },
        setAddModal(state, addModal) {
            state.addModal = { ...state.addModal, ...addModal };
        }
    },
    actions: {
        init({ commit, state }, username) {
            commit('setPageLoading', true);
            // 查用户
            getUser(username).then(userInfo => {
                commit('setUserInfo', userInfo);
                // 查系统
                findModuleByUser(username).then(moduleInfo => {
                    commit('setUserAllModule', moduleInfo);
                    // 查角色
                    findUserAllRole(username).then(rolesInfo => {
                        commit('setRolesTableData', rolesInfo);
                        // 查权限
                        findUserAllPermission(username).then(permissionInfo => {
                            commit('setPermissionTableData', permissionInfo);
                            commit('setPageLoading', false);
                        }).catch(error => commit('setPageLoading', false));
                    }).catch(error => commit('setPageLoading', false));
                }).catch(error => commit('setPageLoading', false));
            }).catch(error => commit('setPageLoading', false));
        },
        findModuleByUser({ commit, state }) {
            commit('setPageLoading', true);
            findModuleByUser(state.userInfo.username).then(moduleInfo => {
                commit('setUserAllModule', moduleInfo);
                commit('setPageLoading', false);
            }).catch(error => commit('setPageLoading', false));
        },
        addUserModuleSubmit({ commit, state, dispatch }) {
            commit('setAddModal', { loading: true });
            addUserModule({ username: state.userInfo.username, module: state.addUserModule.module }).then(data => {
                Message.success({ content: `添加可登录系统成功 -> [${state.addUserModule.module}]`, duration: 3 });
                commit('setAddModal', { show: false, loading: false });
                dispatch('findModuleByUser');
            }).catch(error => commit('setAddModal', { loading: false }));
        },
        delUserModuleSubmit({ commit, state, dispatch }, moduleStr) {
            delUserModule(state.userInfo.username, moduleStr).then(data => {
                Message.success({ content: `移除可登录系统成功 -> [${moduleStr}]`, duration: 3 });
                dispatch('findModuleByUser');
            });
        }
    }
};

export default UserDetail;
